Skip to main content

Java Wrapper Classes - Complete Cheatsheet

A comprehensive guide to Java's wrapper classes with practical code snippets and their use cases.

๐Ÿ“‹ Quick Navigationโ€‹

๐Ÿ”น Integer (for int)โ€‹

Parsing & Conversionโ€‹

// String to int/Integer
Integer.parseInt("123"); // Returns: 123 (primitive int)
Integer.valueOf("123"); // Returns: Integer object
Integer.valueOf("1010", 2); // Binary to int: 10
Integer.decode("0x1A"); // Hex to int: 26

// int to String
Integer.toString(42); // Returns: "42"
Integer.toString(10, 2); // Binary: "1010"
Integer.toString(255, 16); // Hex: "ff"

Math Operationsโ€‹

Integer.max(10, 20);               // Returns: 20
Integer.min(10, 20); // Returns: 10
Integer.sum(10, 20); // Returns: 30
Integer.compare(5, 7); // Returns: -1 (since 5 < 7)
Math.abs(-15); // Returns: 15

Bit Operationsโ€‹

Integer.bitCount(7);               // Number of 1-bits: 3
Integer.toBinaryString(10); // Returns: "1010"
Integer.toHexString(255); // Returns: "ff"
Integer.toOctalString(8); // Returns: "10"
Integer.highestOneBit(12); // Returns: 8 (1000 in binary)
Integer.numberOfLeadingZeros(8); // Returns: 28
Integer.reverse(0b1100); // Reverses bits

Constantsโ€‹

Integer.MAX_VALUE;                 // 2,147,483,647
Integer.MIN_VALUE; // -2,147,483,648
Integer.SIZE; // 32 (bits)

๐Ÿ”น Long (for long)โ€‹

Parsing & Conversionโ€‹

// String to long
Long.parseLong("123456789"); // Returns: 123456789L
Long.valueOf("123456789"); // Returns: Long object
Long.decode("0x1FFFFFFFFL"); // Hex to long

// long to String
Long.toString(12345L); // Returns: "12345"
Long.toString(1024L, 2); // Binary: "10000000000"

Math Operationsโ€‹

Long.max(100L, 200L);              // Returns: 200L
Long.min(100L, 200L); // Returns: 100L
Long.sum(100L, 200L); // Returns: 300L
Long.compare(5L, 7L); // Returns: -1

Bit Operationsโ€‹

Long.bitCount(15L);                // Number of 1-bits: 4
Long.toBinaryString(10L); // Returns: "1010"
Long.toHexString(255L); // Returns: "ff"
Long.numberOfLeadingZeros(16L); // Returns: 59
Long.highestOneBit(12L); // Returns: 8L

Constantsโ€‹

Long.MAX_VALUE;                    // 9,223,372,036,854,775,807
Long.MIN_VALUE; // -9,223,372,036,854,775,808
Long.SIZE; // 64 (bits)

๐Ÿ”น Character (for char)โ€‹

Character Type Checkingโ€‹

Character.isDigit('5');            // true
Character.isLetter('A'); // true
Character.isLetterOrDigit('7'); // true
Character.isUpperCase('A'); // true
Character.isLowerCase('a'); // true
Character.isWhitespace(' '); // true
Character.isAlphabetic('ฮฑ'); // true (Unicode letters)
Character.isDefined('โ‚ฌ'); // true (valid Unicode)

Case Conversionโ€‹

Character.toUpperCase('a');        // Returns: 'A'
Character.toLowerCase('B'); // Returns: 'b'
Character.toTitleCase('a'); // Returns: 'A'

Numeric & Specialโ€‹

Character.getNumericValue('9');    // Returns: 9
Character.getNumericValue('A'); // Returns: 10 (hex)
Character.toString('x'); // Returns: "x"
Character.compare('a', 'b'); // Returns: -1

Constantsโ€‹

Character.MAX_VALUE;               // '\uffff' (65535)
Character.MIN_VALUE; // '\u0000' (0)
Character.SIZE; // 16 (bits)

๐Ÿ”น Boolean (for boolean)โ€‹

Parsing & Conversionโ€‹

Boolean.parseBoolean("true");      // Returns: true
Boolean.parseBoolean("TRUE"); // Returns: true
Boolean.parseBoolean("yes"); // Returns: false (only "true" works)
Boolean.valueOf("false"); // Returns: Boolean.FALSE
Boolean.toString(true); // Returns: "true"

Logical Operationsโ€‹

Boolean.logicalAnd(true, false);   // Returns: false
Boolean.logicalOr(true, false); // Returns: true
Boolean.logicalXor(true, true); // Returns: false
Boolean.compare(true, false); // Returns: 1

Constantsโ€‹

Boolean.TRUE;                      // Boolean object for true
Boolean.FALSE; // Boolean object for false

๐Ÿ”น Double (for double)โ€‹

Parsing & Conversionโ€‹

Double.parseDouble("3.14159");     // Returns: 3.14159
Double.valueOf("2.718"); // Returns: Double object
Double.toString(3.14); // Returns: "3.14"
Double.toHexString(1.0); // Returns: "0x1.0p0"

Special Value Checkingโ€‹

Double.isNaN(Math.sqrt(-1));       // true (Not a Number)
Double.isInfinite(1.0 / 0.0); // true
Double.isFinite(3.14); // true
Double.compare(3.5, 4.5); // Returns: -1

Math Operationsโ€‹

Double.max(3.5, 4.5);              // Returns: 4.5
Double.min(3.5, 4.5); // Returns: 3.5
Double.sum(2.5, 3.5); // Returns: 6.0
Math.abs(-3.14); // Returns: 3.14

Constantsโ€‹

Double.MAX_VALUE;                  // 1.7976931348623157E308
Double.MIN_VALUE; // 4.9E-324 (smallest positive)
Double.POSITIVE_INFINITY; // Positive infinity
Double.NEGATIVE_INFINITY; // Negative infinity
Double.NaN; // Not a Number

๐Ÿ”น Float (for float)โ€‹

Parsing & Conversionโ€‹

Float.parseFloat("3.14f");         // Returns: 3.14f
Float.valueOf("2.718f"); // Returns: Float object
Float.toString(3.14f); // Returns: "3.14"

Special Value Checkingโ€‹

Float.isNaN(0.0f / 0.0f);          // true
Float.isInfinite(1.0f / 0.0f); // true
Float.isFinite(3.14f); // true
Float.compare(3.5f, 4.5f); // Returns: -1

Constantsโ€‹

Float.MAX_VALUE;                   // 3.4028235E38
Float.MIN_VALUE; // 1.4E-45
Float.POSITIVE_INFINITY; // Positive infinity
Float.NEGATIVE_INFINITY; // Negative infinity
Float.NaN; // Not a Number

๐Ÿ”น Byte & Shortโ€‹

Byte Operationsโ€‹

Byte.parseByte("127");             // Returns: 127 (byte)
Byte.toString((byte) 65); // Returns: "65"
Byte.valueOf("42"); // Returns: Byte object
Byte.compare((byte) 5, (byte) 7); // Returns: -1

// Constants
Byte.MAX_VALUE; // 127
Byte.MIN_VALUE; // -128

Short Operationsโ€‹

Short.parseShort("32767");         // Returns: 32767 (short)
Short.toString((short) 1000); // Returns: "1000"
Short.valueOf("500"); // Returns: Short object
Short.compare((short) 5, (short) 7); // Returns: -1

// Constants
Short.MAX_VALUE; // 32767
Short.MIN_VALUE; // -32768

๐Ÿ”น String (Special Reference Type)โ€‹

Length & Checkingโ€‹

String s = "  Hello World  ";
s.length(); // Returns: 15
s.isEmpty(); // Returns: false
"".isEmpty(); // Returns: true
s.isBlank(); // Returns: false (Java 11+)
" ".isBlank(); // Returns: true (Java 11+)

Content Checkingโ€‹

s.contains("Hello");               // Returns: true
s.startsWith(" He"); // Returns: true
s.endsWith("ld "); // Returns: true
s.equals(" Hello World "); // Returns: true
s.equalsIgnoreCase(" HELLO world "); // Returns: true

Manipulationโ€‹

s.trim();                          // Returns: "Hello World"
s.strip(); // Returns: "Hello World" (Java 11+)
s.toUpperCase(); // Returns: " HELLO WORLD "
s.toLowerCase(); // Returns: " hello world "
s.replace("World", "Java"); // Returns: " Hello Java "
s.replaceAll("\\s+", " "); // Regex: replace multiple spaces with single space
s.substring(2, 7); // Returns: "Hello"
s.repeat(3); // Returns: " Hello World Hello World Hello World " (Java 11+)

Splitting & Joiningโ€‹

s.split(" ");                      // Returns: ["", "", "Hello", "World", "", ""]
s.split("\\s+"); // Split on any whitespace: ["", "Hello", "World", ""]
String.join(", ", "A", "B", "C"); // Returns: "A, B, C"
String.join("-", Arrays.asList("X", "Y", "Z")); // Returns: "X-Y-Z"

Indexing & Searchingโ€‹

s.charAt(4);                       // Returns: 'l'
s.indexOf("o"); // Returns: 4 (first occurrence)
s.lastIndexOf("o"); // Returns: 7 (last occurrence)
s.indexOf("World", 5); // Search starting from index 5

Conversionโ€‹

String.valueOf(123);               // Returns: "123"
String.valueOf(3.14); // Returns: "3.14"
String.valueOf(true); // Returns: "true"
String.valueOf(new char[]{'A', 'B'}); // Returns: "AB"
"42".getBytes(); // Returns: byte array

๐Ÿš€ Quick Reference Summaryโ€‹

ClassKey MethodsPrimary Use Cases
IntegerparseInt(), valueOf(), toString(), max(), min(), bitCount()String conversion, math ops, bit manipulation
LongparseLong(), valueOf(), toString(), max(), min()Large number operations, bit manipulation
CharacterisDigit(), isLetter(), toUpperCase(), toLowerCase()Character validation and transformation
BooleanparseBoolean(), valueOf(), logicalAnd(), logicalOr()Boolean parsing and logical operations
Double/FloatparseDouble(), isNaN(), isInfinite(), compare()Decimal number operations, special value checking
Stringtrim(), split(), contains(), substring(), replace()Text manipulation, searching, formatting

๐ŸŽฏ Best Practicesโ€‹

  1. Prefer valueOf() over constructors - Uses cached instances for small numbers
  2. Handle NumberFormatException - Always catch when parsing strings
  3. Use equals() for object comparison - Don't use == with wrapper objects
  4. Consider autoboxing/unboxing overhead - Be mindful in performance-critical code
  5. Null-safe operations - Wrapper objects can be null, primitives cannot
// Good practices
Integer num = Integer.valueOf("123"); // Uses cache
if (Objects.equals(num1, num2)) { } // Null-safe comparison

// Avoid
Integer num = new Integer("123"); // Deprecated, creates new instance
if (num1 == num2) { } // Unsafe reference comparison